package com.alan.wifishare.view.impl.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.libapi.model.business.DeviceStatisticsInfo;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.app.UserConfig;
import com.alan.wifishare.presenter.CNTHaveConnectedPS;
import com.alan.wifishare.utils.FileSizeUtil;
import com.alan.wifishare.view.impl.ActivityJumpUtil;
import com.alan.wifishare.view.impl.service.BusinessService;
import com.alan.wifishare.view.iview.IVCNTHaveConnected;
import com.alan.wifishare.view.ui.CNTConnectedDeviceAdapter;

import java.util.List;

/**
 * @className: CNTHaveConnectedFM
 * @describtion: CNTHaveConnectedFM
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/5/1 19:12
 */
public class CNTHaveConnectedFM extends Fragment implements IVCNTHaveConnected, View.OnClickListener {

    private CNTHaveConnectedPS cntHaveConnectedPS = null;
    private Button cntHaveConnectedManageLinkBtn;
    private TextView itemStatictisNetSpeedTv;
    private TextView itemStatictisTotalFlowsTv;
    private ListView connectedDevicesListView;
    private CNTConnectedDeviceAdapter CNTConnectedDeviceAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.cnt_have_connected_layout, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        cntHaveConnectedPS = new CNTHaveConnectedPS(this);
        initView();
//        cntHaveConnectedPS.getDeviceStatisticsInfo();
        cntHaveConnectedPS.getConnectedDevicesList();
        BusinessService.startService(getActivity());
        cntHaveConnectedPS.sendMasterInfo();
        super.onActivityCreated(savedInstanceState);
    }


    private void initView(){
        cntHaveConnectedManageLinkBtn = (Button)getView().findViewById(R.id.cntHaveConnectedManageLinkBtn);
        cntHaveConnectedManageLinkBtn.setOnClickListener(this);
        itemStatictisNetSpeedTv = (TextView) getView().findViewById(R.id.itemStatictisNetSpeedTv);
        itemStatictisTotalFlowsTv = (TextView) getView().findViewById(R.id.itemStatictisTotalFlowsTv);
        connectedDevicesListView = (ListView) getView().findViewById(R.id.cntConnectedDeviceListview);
        CNTConnectedDeviceAdapter = new CNTConnectedDeviceAdapter(getContext());
        connectedDevicesListView.setAdapter(CNTConnectedDeviceAdapter);
        connectedDevicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ConnectedDeviceInfo connectedDeviceInfo = CNTConnectedDeviceAdapter.getItemList().get(i);
                toastMsg(connectedDeviceInfo.getDeviceName());
                cntHaveConnectedPS.mDebug = true;
                if(i == 1){
                    cntHaveConnectedPS.sendWifiInfo();
                }else if(i == 2){
                    cntHaveConnectedPS.mDebug = false;
                    cntHaveConnectedPS.getConnectedDevicesList();
                }else if(i == 3){
                    cntHaveConnectedPS.deleteDeviceUser(connectedDeviceInfo.getDeviceMac());
                }else if(i == 0){
//                    cntHaveConnectedPS.sendMasterInfo();
                    cntHaveConnectedPS.deleteDeviceUser("doodsdojsdosdoodh");
                }
            }
        });
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toastMsg(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showDeviceStatisticsInfo(DeviceStatisticsInfo deviceStatisticsInfo) {
        itemStatictisNetSpeedTv.setText("当前网速：" + FileSizeUtil.formatFileSize(deviceStatisticsInfo.getNetSpeed()) + "/s");
        itemStatictisTotalFlowsTv.setText("使用总流量：" + FileSizeUtil.formatFileSize(deviceStatisticsInfo.getGpm()));
    }

    @Override
    public void showConnectedDevicesList(List<ConnectedDeviceInfo> connectedDeviceInfos) {
        CNTConnectedDeviceAdapter.setItemList(connectedDeviceInfos);
    }

    @Override
    public void onClick(View view) {
        ActivityJumpUtil.jump2LinkManageAct(getContext(), GlobalField.userConfig.getDeviceSSID());
//        ActivityJumpUtil.jump2ConnectLinkAct(getActivity());
    }
}
